import pygame
import sys
import random

# 初始化pygame
pygame.init()

# 设置屏幕尺寸
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Arkanoid')

# 设置颜色
colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255)]
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)

# 挡板
paddle_width = 100
paddle_height = 10
paddle_x = (screen_width - paddle_width) // 2
paddle_y = screen_height - 50
paddle_speed = 10

# 小球
ball_radius = 10
balls = []

# 砖块
brick_width = 75
brick_height = 20
brick_gap = 5
bricks = []
for row in range(5):
    color = colors[row % len(colors)]
    for col in range(10):
        brick_x = col * (brick_width + brick_gap)
        brick_y = row * (brick_height + brick_gap) + 50
        bricks.append([pygame.Rect(brick_x, brick_y, brick_width, brick_height), color, 0, row])  # [rect, color, hits, row]

# 子弹
bullets = []
bullet_speed = 10

# 字体
font = pygame.font.SysFont('Microsoft YaHei', 36)

# 游戏状态
game_over = False
game_start = False
score = 0
high_score = 0

def reset_game():
    global paddle_x, balls, bricks, bullets, game_over, game_start, score
    paddle_x = (screen_width - paddle_width) // 2
    balls = [{'x': screen_width // 2, 'y': screen_height // 2, 'dx': random.choice([-4, 4]), 'dy': -4}]
    bricks = []
    for row in range(5):
        color = colors[row % len(colors)]
        for col in range(10):
            brick_x = col * (brick_width + brick_gap)
            brick_y = row * (brick_height + brick_gap) + 50
            bricks.append([pygame.Rect(brick_x, brick_y, brick_width, brick_height), color, 0, row])
    bullets = []
    game_over = False
    game_start = True
    score = 0

def draw_score():
    score_text = font.render(f"得分: {score:08d}", True, white)
    screen.blit(score_text, (10, 10))

def show_game_over_screen():
    global high_score
    game_over_text = font.render("游戏结束，按回车重新开始", True, white)
    score_text = font.render(f"总得分: {score}", True, white)
    high_score_text = font.render(f"最高分: {high_score}", True, white)

    screen.blit(game_over_text, (screen_width // 2 - game_over_text.get_width() // 2, screen_height // 2 - 50))
    screen.blit(score_text, (screen_width // 2 - score_text.get_width() // 2, screen_height // 2))
    screen.blit(high_score_text, (screen_width // 2 - high_score_text.get_width() // 2, screen_height // 2 + 50))

def show_new_record_screen():
    new_record_text = font.render("恭喜你创造了新纪录！", True, white)
    screen.blit(new_record_text, (screen_width // 2 - new_record_text.get_width() // 2, screen_height // 2 - 50))

def draw_fireworks():
    for _ in range(10):
        x = random.randint(0, screen_width)
        y = random.randint(0, screen_height)
        radius = random.randint(10, 30)
        color = random.choice(colors)
        pygame.draw.circle(screen, color, (x, y), radius)

# 游戏循环
clock = pygame.time.Clock()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN:
                if not game_start:
                    reset_game()
                elif game_over:
                    if score > high_score:
                        high_score = score
                        show_new_record_screen()
                        draw_fireworks()
                    reset_game()

        elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and game_start and not game_over:  # 鼠标左键
            bullets.append({'x': paddle_x + paddle_width // 2, 'y': paddle_y, 'dy': -10})

    if game_start and not game_over:
        # 获取鼠标位置
        mouse_x, _ = pygame.mouse.get_pos()
        paddle_x = mouse_x - paddle_width // 2

        # 更新小球位置
        for ball in balls[:]:
            ball['x'] += ball['dx']
            ball['y'] += ball['dy']

            # 碰撞检测
            if ball['x'] <= 0 or ball['x'] >= screen_width - ball_radius:
                ball['dx'] *= -1
            if ball['y'] <= 0:
                ball['dy'] *= -1
            if ball['y'] >= screen_height:
                balls.remove(ball)

            paddle_rect = pygame.Rect(paddle_x, paddle_y, paddle_width, paddle_height)
            if paddle_rect.collidepoint(ball['x'], ball['y']):
                ball['dy'] *= -1

            for i, (brick, color, hits, row) in enumerate(bricks[:]):
                if brick.collidepoint(ball['x'], ball['y']):
                    ball['dy'] *= -1
                    hits += 1
                    score += (5 * (4 - row) + 10)  # 倒数第一行10分，每行增加5分
                    if hits == 3:
                        del bricks[i]
                    else:
                        bricks[i][2] = hits
                    break

        # 更新子弹位置
        for bullet in bullets[:]:
            bullet['y'] += bullet['dy']
            if bullet['y'] < 0:
                bullets.remove(bullet)
            else:
                for i, (brick, color, hits, row) in enumerate(bricks[:]):
                    if brick.collidepoint(bullet['x'], bullet['y']):
                        if hits == 2:
                            del bricks[i]
                        else:
                            bricks[i][2] = hits + 1
                        score += (5 * (4 - row) + 10)  # 倒数第一行10分，每行增加5分
                        bullets.remove(bullet)
                        break

        # 小球数量变化
        if len(balls) == 1:
            current_ball = balls[0]
            hit_count = sum([hits for _, _, hits, _ in bricks])
            if hit_count in [3, 5, 10, 27, 30]:
                for _ in range(4):
                    balls.append({'x': current_ball['x'], 'y': current_ball['y'], 'dx': random.choice([-4, 4]), 'dy': -4})

        if not balls:
            game_over = True
        if not bricks:
            game_over = True

    # 绘制屏幕
    screen.fill(black)
    if not game_start:
        text = font.render("按回车开始游戏", True, white)
        text_rect = text.get_rect(center=(screen_width // 2, screen_height // 2))
        screen.blit(text, text_rect)
    elif game_over:
        show_game_over_screen()
        if score > high_score:
            show_new_record_screen()
            draw_fireworks()
    else:
        pygame.draw.rect(screen, white, (paddle_x, paddle_y, paddle_width, paddle_height))
        for ball in balls:
            pygame.draw.circle(screen, red, (ball['x'], ball['y']), ball_radius)
        for brick, color, hits, row in bricks:
            pygame.draw.rect(screen, color, brick)
        for bullet in bullets:
            pygame.draw.rect(screen, white, (bullet['x'], bullet['y'], 2, 10))
        draw_score()

    # 刷新屏幕
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
sys.exit()